//
// Created: May 2000 - Richard Anderson (rja@vivid-creations.com)
//
// Changes: Created for build 1626.  Updated for 1715+
//
// 

using System;
using System.Web.UI;
using System.Collections;

namespace WroxControls 
{


   public class RatingMeter : Control 
   {

      private bool m_bShowRatingText;
      private long m_lCellWidth;
      private long m_lCellHeight;

      private double m_fScore;
      private long m_lVotes;

      private long m_lMaxRating;

      // Initialise defaults to enable simpler page construction (eg. fewer attributes )

      public RatingMeter()
      {
         m_lVotes = 0;
         m_lMaxRating = 5; // default max rating is 5
         m_lCellWidth = 20;
         m_lCellHeight = 5;
         m_bShowRatingText = true;
      }

      // Public accessor to set/get the score so far 

      public double Score
      {
         get { return m_fScore;}
         set {  m_fScore = value; }
       }

      // Public accessor to set/get the votes so far 

      public long Votes
      {
         get { return m_lVotes;}
         set {  m_lVotes = value; }
       }


      // Public accessor to set/get the max rating
      public long MaxRating
      {
         get { return m_lMaxRating;}
         set {  m_lMaxRating = value; }
       }

      // Public accessor to set/get the cell width

      public long CellWidth
      {
         get { return m_lCellWidth;}
         set {  m_lCellWidth = value; }
      }

      // Public accessor to set/get the cell height

      public long CellHeight
      {
         get { return m_lCellHeight;}
         set {  m_lCellHeight = value; }
      }


      // Public accessor to set/get the cell height

      public bool ShowText
      {
         get { return m_bShowRatingText; }
         set {  m_bShowRatingText = value; }
      }
         
      // Draw the rating meter

      protected override void Render(HtmlTextWriter objWriter) 
      { 

         // If there are no votes, so note vote output 
         if  ( m_lVotes == 0 )
         {
            NoRating(objWriter);
            return;
         }

         double fAverageRating;

         fAverageRating = m_fScore / m_lVotes;

         // Perform sanity checks

         if  ( fAverageRating  > m_lMaxRating ) 
         { 
            RaiseError(objWriter, "Average rating exceeds maximum defined");
            return;
         }

//         objWriter.Write("<p>Average vote is " + fAverageRating.ToString() + "</p>");

       
         objWriter.Write("<table border='2' cellpadding='0' cellspacing='0'>");
         objWriter.Write("<tr>");

         long lLoop;

         for ( lLoop =  0; lLoop < m_lMaxRating; lLoop++ )
         {
            objWriter.Write("<td width=" + m_lCellWidth + " height=" + m_lCellHeight + ">");

            if  ( fAverageRating > 0 )
            {

                // Draw a full bar 

                if  ( fAverageRating > 1 )
                {
                   objWriter.Write("<img src='images/reddot.gif' width="  +  m_lCellWidth + " height=" + m_lCellHeight + " border='0' />");
                }
                else
                {
                   long lWidth;
                   long lEmptyWidth;

 
                   lWidth = (long) (m_lCellWidth * fAverageRating);
                
                   lEmptyWidth = m_lCellWidth - lWidth;

                   if  ( lWidth != 0 )
                   {
                      objWriter.Write("<img src='images/reddot.gif' width="  +  lWidth + " height=" + m_lCellHeight + " border='0' />");
                   }

                   if  ( lEmptyWidth != 0 )
                   {
                      objWriter.Write("<img src='images/whitedot.gif' width="  +  lEmptyWidth + " height=" + m_lCellHeight + " border='0' />");
                   }

                }

            }
            else
            {
                objWriter.Write("<img src='images/whitedot.gif' width="  +  m_lCellWidth + " height=" + m_lCellHeight + " border='0' />");
            }
            
            fAverageRating -= 1;
            objWriter.Write("</td>");
         }

         objWriter.Write("</tr>");



         objWriter.Write("</table>");

         // Show rating text if requested

         if  ( m_bShowRatingText == true )
         {
            objWriter.Write("<p>Adventure Works Rating " + m_fScore.ToString() + " out of a possible " + m_lMaxRating * m_lVotes + "</p>");
         }
      }

      // Draw output for no votes 

      protected void NoRating(HtmlTextWriter objWriter)
      {
         objWriter.Write("<p>No votes for this item yet</p>");   
      }
 
      // Raise Error

      protected void RaiseError(HtmlTextWriter objWriter, string sError)
      {
         objWriter.Write("<p>Error:" + sError + "</p>");   
      }

   }    
}